(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:ManagementCtrl
     * @description
     * # ManagementCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('ManagementCtrl', ManagementCtrl);

    ManagementCtrl.$inject = ['$scope', 'dataService', 'helper', '$location', '$compile', ];

    function ManagementCtrl($scope, dataService, helper, $location, $compile) {

        $scope.listAction = function() {
            // console.error("moment.tz.guess()",moment.tz.guess())
            $scope.xtotal = 0;
            $scope.pageSize = 10;
            $scope.getList();
        }

        $scope.getList = function(pageNo) {
            $scope.pageNo = pageNo || 1;
            var query = {
                page_no: $scope.pageNo,
                page_size: $scope.pageSize,
                query: {
                    service_type_id: 2
                },
            };
            if ($scope.template_id) {
                query.query.template_id = $scope.template_id;
            }
            if ($scope.appid) {
                query.query.appid = $scope.appid;
            }
            if ($scope.nick_name) {
                query.query.nick_name = $scope.nick_name;
            }
            if ($scope.principal_name) {
                query.query.principal_name = $scope.principal_name;
            }

            dataService.custaccountGet(query).then(function(list) {
                if (list && list.cust_account) {
                    async.map(list.cust_account, function(data, callback) {
                        var query = {
                            partner_id: data.partner_id
                        };
                        dataService.v1PartnerGet(query).then(function(lit) {
                            if (!lit.Msg) {
                                data.partner = lit;
                                callback(null, null);
                            } else {
                                callback(null, null);
                            }
                        });
                    }, function(e, r) {
                        $scope.authrizeApp = list.cust_account;
                        $scope.xtotal = list.total_count;
                    });
                }
            });
        }

        $scope.addAction = function() {
            var appid = $location.search()["appid"];
            if (appid) {
                dataService.isvWxappOne(appid).then(function(result) {
                    if (result.appid) {
                        $scope.wxapp = result;
                    } else {
                        helper.myAlert("错误：" + result.Msg);
                    }
                })
            } else {
                $scope.wxapp = {
                    app_type: 1,
                    template_id: 1,
                    statu: -1,
                    version: "v0.0.1",
                    description: "描述",
                }
            }
        }

        //小程序二维码
        function wxappQrcode(wxappid) {
            return dataService.acodeunlimitGet(wxappid, "pcadmin", true, "").then(function(r) {
                return r;
            });
        }

        //s使用用户模板到管理员中
        $scope.savelayout = function(id) {
            if (id) {
                $.alert({
                    backdrop: 'static',
                    keyboard: true,
                    title: '复制模板',
                    body: '<div>模板名称: <input ng-model="fuzhiname" type="text"  ></div>',
                    okBtn: '{{"ok" | translate}}',
                    closeBtn: true,
                    transition: true,
                    // width: 'small',
                    height: "200px",
                    show: function(e) {
                        $compile($(e.currentTarget).contents())($scope);
                        return true;
                    },
                    okHide: function(e) {
                        if (!$scope.fuzhiname) {
                            helper.myAlert("模板名称不能为空");
                            return false;
                        } else {
                            $scope.savefuzhimb(id);
                        }

                    },
                });
            }
        }

        //保存动作
        $scope.savefuzhimb = function(id) {
                //查询首页模板
                var data = {
                    appid: id,
                    name: "UI-MOd-home",
                }
                dataService.wxappOne(data).then(function(list) {
                    if (list) {
                        $scope.content = JSON.parse(list.value);
                        $scope.addAdminLayout(id);
                    } else {
                        return helper.myAlert("该小程序没有装修页面");
                    }
                });
            }
            //修改支付方式
        $scope.upPaytype = function(mch_type_id, data) {
            var pdata = {
                cust_account_id: data.cust_account_id,
                mch_type_id: mch_type_id,
            }
            pdata.pay_way_id = mch_type_id == 3 ? 1 : 2;
            pdata.pay_way_id = mch_type_id == 4 ? 9 : pdata.pay_way_id;
            pdata.pay_way_id = mch_type_id == 5 ? 10 : pdata.pay_way_id;
            pdata.pay_way_id = mch_type_id == 6 ? 13 : pdata.pay_way_id;
            pdata.pay_way_id = mch_type_id == 7 ? 11 : pdata.pay_way_id;
            pdata.pay_way_id = mch_type_id == 8 ? 12 : pdata.pay_way_id;
            pdata.pay_way_id = mch_type_id == 9 ? 14 : pdata.pay_way_id;
            pdata.pay_way_id = mch_type_id == 10 ? 16 : pdata.pay_way_id;
            pdata.pay_way_id = mch_type_id == 11 ? 15 : pdata.pay_way_id;
            pdata.pay_way_id = mch_type_id == 12 ? 17 : pdata.pay_way_id;
            pdata.pay_way_id = mch_type_id == 18 ? 17 : pdata.pay_way_id;
            dataService.payConfigGet(data.cust_account_id).then(function(result) {
                if (result) {
                    $scope.payconfig = result;
                    pdata.pay_params_id = result.pay_params_id;
                    dataService.payConfigUpdate(pdata).then(function(result) {
                        if (result.pay_params_id) {
                            helper.message("成功");
                        }
                    });
                } else {
                    pdata.app_id = data.app_id;
                    pdata.mch_id = "1";
                    pdata.name = "1";
                    pdata.appkey = "1";
                    pdata.media_id = "";
                    pdata.status_id = "1";
                    pdata.sub_app_id = "1";
                    pdata.sub_mch_id = "1";
                    dataService.payConfigAdd(pdata).then(function(result) {
                        if (result.pay_params_id) {
                            helper.message("成功");
                        } else {
                            helper.message("参数不全！");
                        }
                    });
                }
            });

        }

        $scope.addAdminLayout = function(id) {

            var saveData = {
                content: {
                    // admin_layout_id: $scope.wcid,
                    name: $scope.fuzhiname,
                    value: $scope.content,
                    page: "home",
                }
            };

            dataService.adminlayoutAdd(saveData).then(function(list) {
                if (list.admin_layout_id) {
                    return helper.myAlert("成功！");
                }
            });
        }

        //弹出小程序二维码;
        $scope.openQrcode = function(app_id) {
            wxappQrcode(app_id).then(function(image) {
                // console.error("二维码数据：", image);
                if (!image) {
                    //$scope.authUrl();
                    helper.myAlert("出错了！");
                    return false
                }

                $.alert({
                    backdrop: "static",
                    keyboard: false,
                    title: '<span>小程序二维码，请使用微信扫描</span>',
                    body: '<div style="text-align:center;"><img src="data:image/png;base64,' + image + '"  width="240"/></div>',
                    okBtn: '{{"二维码"|translate}}',
                    closeBtn: true,
                    hasfoot: false,
                    transition: true,
                    width: 'small',
                    show: function(e) {},
                    okHide: function(e) {

                    },
                    shown: function(e) {}
                });
            });
        }

        //短信日志
        $scope.msglogAction = function() {
            $scope.pageSize = 10;

            $scope.getLogList(1);
        }

        $scope.getLogList = function(pageNo) {
            var user_id = $location.search().user_id;
            $scope.pageNo = pageNo || 1;
            var query = {
                page_no: $scope.pageNo,
                page_size: $scope.pageSize,
                user_id: user_id,
            };

            dataService.mbMsgLog(query).then(function(list) {
                if (list && list.total_count) {
                    $scope.log_list = list.log_list;
                    $scope.log_count = list.total_count;
                }
            });
        }
    }
})();